const app = getApp()
let qCloud = require("../../index.js");

Page({
  /** 页面的初始数据 */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    goodsId: null, // 货物ID
    goodsName: "", // 货物名称(货号)
    goodsCode: "", // 货物编号
    goodsShelves: "", // 货架号
    goodsTotalNumber: "", // 货物总数量
    goodsTotalAmount: "", // 货物总金额
    specsType: "", // 尺码类型
    specsTypeTitle: "", // 尺码类型标题
    cooperateEnterpriseId: "", // 合作企业ID
    cooperateEnterpriseName: "", // 合作企业名称
    warehouseId: "", // 仓库ID
    warehouseName: "", // 仓库名称
    imageId: "", // 图片
    imageUrl: "", // 图片
    remark: "", // 备注
    specsTypeList: [], // 货物规格列表
  },

  /** 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    let self = this;
    self.setData({
      goodsId: options.goodsId
    });

    // 查询仓储货物详情
    self.queryGoodsDetails();
  },

  /** 查询仓储货物详情 */
  queryGoodsDetails: function () {
    let self = this

    let params = {
      goodsId: self.data.goodsId
    }

    app.fetchApis(self, qCloud.api.warehouse_queryGoodsDetails, params, "POST", function (e) {
      let result = e.data;

      if (result.code == 200) {
        let data = result.data;

        self.setData({
          goodsName: data.goodsName, // 货物名称(货号)
          goodsCode: data.goodsCode, // 货物编号
          goodsShelves: data.goodsShelves, // 货架号
          goodsTotalNumber: data.goodsTotalNumber, // 货物总数量
          goodsTotalAmount: data.goodsTotalAmount, // 货物总金额
          specsType: data.specsType, // 尺码类型
          warehouseId: data.warehouseId, // 仓库ID
          warehouseName: data.warehouseName, // 仓库名称
          imageId: data.imageId, // 图片
          remark: data.remark, // 备注
          cooperateEnterpriseId: data.cooperateEnterpriseId, // 合作企业ID
          cooperateEnterpriseName: data.cooperateEnterpriseName, // 合作企业名称
        });

        // 获取图片信息
        self.getFilesUrl(data.imageId);

        // 尺码类型列表
        self.convertSpecsTypeList(data.goodsSpecsList);

        // 尺码类型
        self.convertSpecsTypeTitle(data.specsType);
      }
    });
  },

  // 获取图片信息
  getFilesUrl: function (imageId) {
    let self = this;
    let params = {
      filesId: imageId
    }

    app.fetchApis(self, qCloud.api.getFileUrl, params, "GET", function (e) {
      let result = e.data;
      if (result.code == 200) {
        let data = result.data;
        self.setData({
          imageUrl: data.filesUrl,
        });
      }
    });
  },

  // 转换尺码类型列表
  convertSpecsTypeList(goodsSpecsList) {
    let self = this;

    let specsTypeList = [];
    let colourIndex = [];
    for (let i = 0; i < goodsSpecsList.length; i++) {
      let goodsSpecs = goodsSpecsList[i];

      if (colourIndex.length > 0) {
        let ci = -1;
        for (let j = 0; j < colourIndex.length; j++) {
          if (goodsSpecs.colour == colourIndex[j]) {
            ci = j;
            break;
          }
        }

        if (ci == -1) {
          colourIndex.push(goodsSpecs.colour);

          let specsList = [{
            specs: goodsSpecs.specs,
            inventory: goodsSpecs.inventory,
            unitPrice: goodsSpecs.unitPrice,
            totalPrice: goodsSpecs.totalPrice,
          }];
          let specsTypeInfo = {
            colour: goodsSpecs.colour,
            specsList: specsList
          };
          specsTypeList.push(specsTypeInfo);
        } else {
          let specsInfo = {
            specs: goodsSpecs.specs,
            inventory: goodsSpecs.inventory,
            unitPrice: goodsSpecs.unitPrice,
            totalPrice: goodsSpecs.totalPrice,
          };
          specsTypeList[ci]["specsList"].push(specsInfo);
        }
      } else {
        colourIndex.push(goodsSpecs.colour);

        let specsList = [{
          specs: goodsSpecs.specs,
          inventory: goodsSpecs.inventory,
          unitPrice: goodsSpecs.unitPrice,
          totalPrice: goodsSpecs.totalPrice,
        }];
        let specsTypeInfo = {
          colour: goodsSpecs.colour,
          specsList: specsList
        };
        specsTypeList.push(specsTypeInfo);
      }
    }

    self.setData({
      specsTypeList: specsTypeList // 尺码类型列表
    });
  },
  // 转换尺码类型
  convertSpecsTypeTitle(specsType) {
    let self = this;
    let specsTypeTitle = "";
    if (specsType == "SML") {
      specsTypeTitle = "SML尺码";
    } else if (specsType == "NS") {
      specsTypeTitle = "数字尺码";
    } else if (specsType == "CS") {
      specsTypeTitle = "自定义尺码";
    }
    self.setData({
      specsTypeTitle: specsTypeTitle
    });
  },
  /** 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {},
  /** 生命周期函数--监听页面显示 */
  onShow: function () {},
  /** 生命周期函数--监听页面隐藏 */
  onHide: function () {},
  /** 生命周期函数--监听页面卸载 */
  onUnload: function () {},
  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {},
  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {},
  /** 用户点击右上角分享 */
  onShareAppMessage: function () {}
});
